	var index = -1; //Index of selected answer
	var aindex = 1; //Index of current question
	var testCount = 0; //Count of all test
	
	var categoryQuestion =0;
	var cq = false;
	var p_id = 0;
	var fillQuestion = 0;
	
	var types = new Array(); // Array of types
	var ids = new Array();
	var answer1 = new Array(); //Array of answer1
	var answer2 = new Array(); //Array of answer2
	var answer3 = new Array(); //Array of answer3 
	var bairshuulah = 0;
	var num = 0,timeT=0,test_id=0,pass_c = 0,exam_id=0;
	
	
	$(document).ready(function(){
		
		startTimer(timeT); //Tsag ehluulj bna
	 	resizeVertDiv('scroll',91); //Shalgaltiin huudasnii tohirgoo hiij bna
		checktimer(); //tsag shalgaj bna

		$(".test2,.test1,.test3,.test5,.test6,.test1,#back, #finish , #timeOn, #cat ,.allTest .test6 ul li span" ).hide();
		$("#"+aindex).show();
		$('.allTest .test1 ul li').click( function(){
			$('.allTest .test1 .' + aindex+' li.selected').removeClass('selected');
			$(this).addClass('selected').children("input[@type=radio]").click();
			index = $('.allTest .test1 .' + aindex+' li').index( this );
			setAnswer1(aindex,index);
		});
		
		$('.allTest .test2 ul li').click( function(){
			dec = $('.allTest .test2 .' + aindex+' li').index( this );
			
			if(index==dec){}
			else{
			$('.allTest .test2 .' + aindex+' li.first').removeClass('first').children().checked=false;
			$('.allTest .test2 .' + aindex+' li.last').removeClass('last').addClass('first');
			$(this).removeClass('last').removeClass('first').addClass('last').children("input[@type=radio]").click();
			index = $('.allTest .test2 .' + aindex+' li').index( this );
			setAnswer2(aindex,index)
		}
		});
		$('.allTest .test3 ul li').click( function(){
			dec = $('.allTest .test3 .' + aindex+' li').index( this );
			
			if(index==dec){}
			else{
			
			$('.allTest .test3 .' + aindex+' li.first').removeClass('first');
			$('.allTest .test3 .' + aindex+' li.middle').removeClass('middle').addClass('first');
			$('.allTest .test3 .' + aindex+' li.last').removeClass('last').addClass('middle');
			$(this).removeClass('last').removeClass('middle').removeClass('first').addClass('last');
			index = $('.allTest .test3 .' + aindex+' li').index( this );
			setAnswer3(aindex,index)
		}
		});
	$('.allTest .test5 ul li .secA').click( function(){
			 $(this).closest('li').addClass('hide');
			$('#secA').append('<li choices='+$(this).closest('li').attr("id")+'> ' +$(this).closest('li').attr("text") +'<button class="delete">X</button></li>'); 
		});
		$('.allTest .test5 ul li .secB').click( function(){
			$(this).closest('li').addClass('hide');
			$('#secB').append('<li choices='+$(this).closest('li').attr("id")+'> ' +$(this).closest('li').attr("text") +'<button class="delete">X</button></li>'); 
		});
		$('.allTest .test6 ul li').click( function(){
			$('.allTest .test6 ul li').children(".sho").css("display","inline").removeClass("sho").hide();
			$(this).find("span").addClass("sho").css("display","block");
			$('.allTest .test6 ul li .sho').show();
			index = $('.allTest .test6 .' + aindex+' li').index( this );
			setAnswer1(aindex,index);
		});
		$("#help").click(function () { 
			$(".hide").removeClass("hide");
			
	    });
	    $('.delete').live('click', function() {
		 var a	=$(this).closest('li').attr("choices");
			$("#"+a).removeClass("hide");
			$(this).closest('li').remove();
			 
			});
	 
		$("#next").click(function () { 
			$("#"+aindex).hide();
			aindex++;
			$("#"+aindex).show();
			index=-1;
			if(aindex==testCount){
				$('#next').hide();
				$("#finish").show();
				}
			if(aindex==2){
				$("#back").show();
				}
			if(aindex==categoryQuestion){
				$("#sss").hide();
				$("#cat").show();
				}
			if(aindex==categoryQuestion+1  ){
				$("#sss").show();
				$("#cat").hide();
				}
			
	    });
	    $("#back").click(function () { 
			$("#"+aindex).hide();
			if(aindex==testCount){
				$("#next").show();
				$("#finish").hide();
				}
			aindex--;
			$("#"+aindex).show();
			index=-1;
			if(aindex==1){
				$("#back").hide();
				}
				if(aindex==categoryQuestion){
				$("#sss").hide();
				$("#cat").show();
				}
			if(aindex==categoryQuestion-1 |aindex==categoryQuestion+1  ){
				$("#sss").show();
				$("#cat").hide();
				}
	    });
	    $("#show").click(function (){
			alert(index);
			});
	
		$("#review").click(function (){
			
				alert(cq);
			/*var timeR =minRemain * 60 + secsRemain;
			$.post("http://127.0.0.1:8000/exam/xhr_test", {'exam_id':exam_id, 'types': types,'answer1':answer1,'answer2':answer2,'answer3':answer3,'ids':ids,'time':timeR,'test_id':test_id,'pass_c':pass_c},
				function(data) {
				});*/
				
			});
		$("#finish").click(function (){
			var as =  "";
			var sa = "";
		$('.catA li').each(function(index) {
			as = as + $(this).attr("choices") + " ";
		  });
		  $('.catB li').each(function(index) {
			sa = sa + $(this).attr("choices") + " ";
		  });
			var timeR = minRemain * 60 + secsRemain;
		
			$.post("http://127.0.0.1:8000/exam/finish", { 'exam_id':exam_id,'types': types,'answer1':answer1,'answer2':answer2,'answer3':answer3,'ids':ids,'time':timeR,'test_id':test_id,'pass_c':pass_c,'cat_q':cq,'cat_ansA':as,'cat_ansB':sa,'p_id':p_id},
				function(data) {
					window.location = "http://127.0.0.1:8000/exam/" + data + "/ex"
				});
			});
		$("#timeOff").click(function (){
				$("#timeVal").hide();
				$("#timeOff").hide();
				$("#timeOn").show();
			});
		$("#timeOn").click(function (){
				$("#timeVal").show();
				$("#timeOn").hide();
				$("#timeOff").show();
			});
	  
	            
	          
	                
	});
	
		
	function product(a,b)
	{
		testCount = a;
		var i=0;
	for (i=0;i<testCount;i++)
	{
	answer1[i]=-1;
	types[i] = -1; 
	answer2[i] = -1;
	answer3[i] = -1;
	ids[i] = -1;
	
	}
	return a;
	}
	//4-s 1-g songoh testnii hariult set hiih
	function setAnswer1(a,b){
		answer1[a-1] = index+1;
		}
	//4-s 2-g songoh testiin hariultiig set hiij bna
	function setAnswer2(a,b){
		answer2[a-1] = answer1[a-1];
		answer1[a-1] = index+1;
		}
	function setAnswer3(a,b){
		answer3[a-1] = answer2[a-1];
		answer2[a-1] = answer1[a-1];
		answer1[a-1] = index+1;
		}
	
	
	
